﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace FrbaCommerce.Abm_Cliente
{
    public partial class frm_clientes : Form
    {
        public frm_clientes()
        {
            InitializeComponent();
        }

        private void frm_clientes_Load(object sender, EventArgs e)
        {
            BaseDeDatos baseDeDatos = BaseDeDatos.Instance;
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.CommandType = CommandType.StoredProcedure;
            sqlCommand.CommandText = "LOS_GDDES.SP_TRAER_TIPOS_DOCUMENTO";
            DataTable dt_tiposDocumento = baseDeDatos.consulta(sqlCommand);
            cmb_tipoDocCliente.DataSource = dt_tiposDocumento;
            cmb_tipoDocCliente.DisplayMember = "DESCRIPCION";
            cmb_tipoDocCliente.ValueMember = "ID_TIPO_DOC";
        }

        private void btn_limpiar_Click(object sender, EventArgs e)
        {
            gb_filtrosBusqueda.Controls.OfType<TextBox>().ToList().ForEach(textBox => textBox.Text = String.Empty);
            gb_filtrosBusqueda.Controls.OfType<ComboBox>().ToList().ForEach(comboBox => comboBox.Text = String.Empty);
        }

        private void btn_buscar_Click(object sender, EventArgs e)
        {
            BaseDeDatos baseDeDatos = BaseDeDatos.Instance;
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "LOS_GDDES.SP_BUSCAR_CLIENTES";
            sqlCommand.CommandType = CommandType.StoredProcedure;
            if (txt_nombreCliente.Text != "")
                sqlCommand.Parameters.AddWithValue("@nombre", txt_nombreCliente.Text);
            if (txt_apellidoCliente.Text != "")
                sqlCommand.Parameters.AddWithValue("@apellido", txt_apellidoCliente.Text);
            if (txt_emailCliente.Text != "")
                sqlCommand.Parameters.AddWithValue("@email", txt_emailCliente.Text);
            sqlCommand.Parameters.AddWithValue("@id_tipo_doc", Convert.ToInt32(cmb_tipoDocCliente.SelectedValue));
            if (txt_nroDoc.Text != "")
                sqlCommand.Parameters.AddWithValue("@nro_doc", Convert.ToInt32(txt_nroDoc.Text));
            
            DataTable dt_clientes = baseDeDatos.consulta(sqlCommand);
            dgv_clientes.DataSource = dt_clientes;

            dgv_clientes.Columns[2].Visible = false;
            dgv_clientes.Columns[3].Visible = false;
            dgv_clientes.Columns[6].Visible = false;
        }

        private void dgv_clientes_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            btn_modificar.Enabled = true;
            btn_eliminar.Enabled = true;
        }

        private void btn_crear_Click(object sender, EventArgs e)
        {
            frm_crearCliente frm_crearCliente = new frm_crearCliente();
            frm_crearCliente.ShowDialog();
            this.btn_buscar_Click(sender,e);
        }

        private void btn_modificar_Click(object sender, EventArgs e)
        {        
              frm_modificarCliente frm_modificarCliente = new frm_modificarCliente(dgv_clientes.CurrentRow);
              frm_modificarCliente.ShowDialog();
              this.btn_buscar_Click(sender, e);
        }

        private void btn_eliminar_Click(object sender, EventArgs e)
        {
            DataRow cliente = (dgv_clientes.CurrentRow.DataBoundItem as DataRowView).Row;

            BaseDeDatos baseDeDatos = BaseDeDatos.Instance;
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "LOS_GDDES.SP_USUARIO_X_ROL_BAJA_LOGICA";
            sqlCommand.CommandType = CommandType.StoredProcedure;
            sqlCommand.Parameters.AddWithValue("id_usuario", cliente.Field<Decimal>("ID_USUARIO"));
            sqlCommand.Parameters.AddWithValue("id_rol", 1);
            baseDeDatos.ejecuta(sqlCommand);
            this.btn_buscar_Click(sender, e);
        }
    }
}
